import asyncio


async def heat_up_water():
    print("开始烧水")
    await asyncio.sleep(5)
    print("结束烧水")
    return "开水"

async def wash_cups():
    print("开始洗杯子")
    await asyncio.sleep(2)
    print("结束洗杯")
    return "茶杯"

async def make_tea_serial():
    """
    串行泡茶
    ===================================
    准备沏茶
    开始烧水
    结束烧水
    开水 就绪
    开始洗杯子
    结束洗杯
    茶杯 就绪
    :return:
    """
    print("准备沏茶")
    rs = await heat_up_water()
    print(rs,"就绪")
    rs = await wash_cups()
    print(rs, "就绪")


async def make_tea_parallel():
    """
    并行泡茶
    ==================================
    准备沏茶
    开始烧水
    开始洗杯子
    结束烧水
    结束洗杯
    开水 就绪
    茶杯 就绪
    泡茶结束
    :return:
    """
    print("准备沏茶")
    task1 = asyncio.create_task(heat_up_water())
    task2 = asyncio.create_task(wash_cups())
    rs = await task1
    print(rs,"就绪")
    rs = await task2
    print(rs, "就绪")
    print("泡茶结束")


async def make_tea_parallel_advance():
    """
    并行泡茶进阶版
    ==================================
    准备沏茶
    开始烧水
    开始洗杯子
    结束洗杯
    结束烧水
    {<Task finished name='Task-2' coro=<heat_up_water() done, defined at D:\Study\Python\async\src\04_await.py:4> result='开水'>, <Task finished name='Task-3' coro=<wash_cups() done, defined at D:\Study\Python\async\src\04_await.py:10> result='茶杯'>} 就绪
    set() 等待中
    泡茶结束
    :return:
    """
    print("准备沏茶")
    tasks = [
        asyncio.create_task(heat_up_water(),name="烧水"),
        asyncio.create_task(wash_cups(),name="洗杯")
    ]
    done,pending = await  asyncio.wait(tasks,timeout=7)
    if len(done) >0:
        #print(done,"就绪")
        for t in done:
            print(t.get_name(),t.result(),"就绪")
    if len(pending) > 0:
        print(pending, "等待中")
    print("泡茶结束")

asyncio.run(make_tea_parallel_advance())